/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kelin.calendarlistview;

import com.bumptech.glide.Glide;

import com.kelin.calendarlistview.library.BaseCalendarListAdapter;
import com.kelin.calendarlistview.library.CalendarHelper;

import ohos.agp.components.*;

import ohos.app.Context;

import ohos.utils.net.Uri;

import java.util.Calendar;

/**
 * DayNewsListAdapter
 *
 * @since 2021.07.28
 */
public class DayNewsListAdapter extends BaseCalendarListAdapter<News.StoriesBean> {

    /**
     * DayNewsListAdapter
     *
     * @param context 上下文
     */
    public DayNewsListAdapter(Context context) {
        super(context);
    }

    @Override
    public Component getSectionHeaderView(String date, Component convertView, ComponentContainer parent) {
        HeaderViewHolder headerViewHolder;
//        List<News.StoriesBean> modelList = dateDataMap.get(date);
        if (convertView != null && !(convertView.getTag() instanceof HeaderViewHolder)) {
            convertView.release();
            convertView = null;
        }
        if (convertView != null) {
            headerViewHolder = (HeaderViewHolder) convertView.getTag();
        } else {
            convertView = inflater.parse(ResourceTable.Layout_listitem_calendar_header, null, false);
            headerViewHolder = new HeaderViewHolder();
            headerViewHolder.dayText = (Text) convertView.findComponentById(ResourceTable.Id_header_day);
            headerViewHolder.yearMonthText = (Text) convertView.findComponentById(ResourceTable.Id_header_year_month);
//            headerViewHolder.isFavImage = (Image) convertView.findComponentById(ResourceTable.Id_header_btn_fav);
            convertView.setTag(headerViewHolder);
        }

        Calendar calendar = CalendarHelper.getCalendarByYearMonthDay(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dayStr = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        if (day < 10) {
            dayStr = "0" + dayStr;
        }
        headerViewHolder.dayText.setText(dayStr);
        headerViewHolder.yearMonthText.setText(CalendarAbilitySlice.YEAR_MONTH_FORMAT.format(calendar.getTime()));
        return convertView;
    }

    @Override
    public Component getItemView(News.StoriesBean model, String date, int pos, Component convertView, ComponentContainer parent) {
        ContentViewHolder contentViewHolder;
        if (convertView != null && !(convertView.getTag() instanceof ContentViewHolder)) {
            convertView.release();
            convertView = null;
        }
        if (convertView != null) {
            contentViewHolder = (ContentViewHolder) convertView.getTag();
        } else {
            convertView = inflater.parse(ResourceTable.Layout_listitem_calendar_content, null, false);
            contentViewHolder = new ContentViewHolder();
            contentViewHolder.titleTextView = (Text) convertView.findComponentById(ResourceTable.Id_title);
            contentViewHolder.timeTextView = (Text) convertView.findComponentById(ResourceTable.Id_time);
            contentViewHolder.newsImageView = (Image) convertView.findComponentById(ResourceTable.Id_image);
            contentViewHolder.itemAll = (DirectionalLayout) convertView.findComponentById(ResourceTable.Id_item_all);
            convertView.setTag(contentViewHolder);
        }

        contentViewHolder.titleTextView.setText(model.getTitle());
        contentViewHolder.timeTextView.setText(date);
        Glide.with(convertView.getContext())
                .load(Uri.parse(model.getImages().get(0)))
                .into(contentViewHolder.newsImageView);
        RippleUtil.setComponent(contentViewHolder.itemAll, 5, 0xAAAAAA);
        return convertView;
    }

    private static class HeaderViewHolder {
        Text dayText;
        Text yearMonthText;
//        Image isFavImage;
    }

    private static class ContentViewHolder {
        Text titleTextView;
        Text timeTextView;
        Image newsImageView;
        DirectionalLayout itemAll;
    }

}
